// app.controller('NewCampusbulidingModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
//         function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
//             $scope.items = ['item1', 'item2', 'item3'];
//             //console.log($state.params);
//             var campus_name, building_name, layer_amount,type,layersJson=[],responser,phone;
//             var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
//                 $scope.oo = {};
//                 $scope.items = items;
//                 $scope.selected = {
//                     item: $scope.items[0]
//                 };
//                 $scope.ok = function () {
//                     campus_name = $scope.oo.campus_name;
//                     building_name = $scope.oo.building_name;
//                     layer_amount = $scope.oo.layer_amount;
//                     type =   $scope.oo.type;
//                     layersJson =  $scope.oo.layersJson; 
//                     responser =  $scope.oo.responser;
//                     phone = $scope.oo.phone;
//                     $modalInstance.close($scope.selected.item);
//                 };
                
//                 $scope.cancel = function () {
//                     $modalInstance.dismiss('cancel');
//                 };
//             };
//             $scope.open = function (size) {
//                 var modalInstance = $modal.open({
//                     templateUrl: 'newcampusbuildingModalContent.html',
//                     controller: ModalInstanceCtrl,
//                     size: size,
//                     resolve: {
//                         items: function () {
//                             return $scope.items;
//                         }
//                     }
//                 });
//                 modalInstance.result.then(function (selectedItem) {
                    
//                     $http.post(
//                         $scope.app.backendurl+"tbl-campus-building/create",
//                         {
//                             'school_id':$state.params.campusid,
//                             'campus_name': campus_name,
//                             'building_name': building_name,
//                             'layer_amount': layer_amount,
//                             'type':type,
//                             'layersJson':layersJson,
//                             'responser':responser,
//                             'phone':phone,     
//                             'token':$localStorage.token                   

//                         }
//                     ).success(function (data) {
//                       //console.log(data);
//                       if(data.code==0&&data.msg=='楼创建成功'){
//                         alert(data.msg);
//                         $window.location.reload('app/sys/campusconfig');                      

//                       }
//                       if(data.code==1){
//                         alert(data.msg);
//                       }
//                        if(data.code==20001){
//                          $location.path('access.signin');
//                        }
//                     }).error(function (data) {
//                         $scope.authError = data.error.code;
//                         if ($scope.authError == 401) {
//                             $location.path('access.signin');
//                         }
//                     })
//                 });
//             };
//         }])

app.controller('NewCampusbulidingTypeModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];         
            var campus_name, building_name, layer_amount,type,layersJson=[],responser,phone;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};

                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $http.post(
                    "http://118.178.136.209/index.php?r=tbl-campus-building/view-all",
                    {
                        'school_id':$state.params.campusid,                       
                        'token':$localStorage.token                  

                    }
                ).success(function (data) {                  
                    if(data.code==0&&data.msg=='ok'){
                        if(data.data.type[0].type_name=='无'){
                            data.data.type = [];
                        }
                        $scope.typeData = data.data.type;

                         


                    }
                    if(data.code==20001&&data.msg=="请先登录"){
                        $location.path('access.signin');
                    }

                 }).error(function (data) {
                       alert(data);
                 })
     
                $scope.tid = 0;
                $scope.addinput = function(){
                     $scope.oo.addinput = true;
                }
                $scope.addsave = function(){  
                    var create_type = $scope.oo.create_types.replace(/，/ig,',')
                    var create_types = create_type.split(',');  
                    $http.post(
                        "http://118.178.136.209/index.php?r=tbl-campus-building/modify-types",
                        {
                            'school_id':$state.params.campusid, 
                            'create_types':JSON.stringify(create_types),                   
                            'token':$localStorage.token                  

                        }
                    ).success(function (data) {
                       // console.log(data);
                        if(data.code==0&&data.msg=="修改成功"){
                            alert(data.msg);
                            $window.location.reload("app.sys.campusconfig")

                        }
                        if(data.code==1){
                            alert(data.msg)
                        }
                        if(data.code==20001&&data.msg=="请先登录"){
                            $location.path('access.signin');
                        }

                     }).error(function (data) {
                           alert(data);
                     })
                }
                $scope.a = 0;$scope.b=0;
                $scope.edit = function(mid){
                    // console.log(tid);
                    $scope.tid = mid;
                     // console.log($scope.tid);
                    $scope.a=1;
                    $scope.b=1;
                }
                $scope.editsave = function(sid){                   
                    var update_types = [];
                    var update_type = {};
                    update_type.id = sid;
                    update_type.type_name = $scope.oo.typename;
                    update_types.push(update_type);
                    $http.post(
                        "http://118.178.136.209/index.php?r=tbl-campus-building/modify-types",
                        {
                            'school_id':$state.params.campusid, 
                            'update_types':JSON.stringify(update_types),                   
                            'token':$localStorage.token                  

                        }
                    ).success(function (data) {
                      // console.log(data);
                        if(data.code==0&&data.msg=="修改成功"){
                            alert(data.msg);
                            $window.location.reload("app.sys.campusconfig")

                        }
                        if(data.code==1){
                            alert(data.msg);
                        }
                        if(data.code==20001&&data.msg=="请先登录"){
                            $location.path('access.signin');
                        }

                     }).error(function (data) {
                           alert(data);
                     })

                }
                $scope.quxiao = function(){
                    $scope.a = 0;$scope.b=0;
                }

                $scope.del = function(did){
                    var delete_types = [];
                    var delete_type = did;
                    delete_types.push(delete_type);
                    $http.post(
                        "http://118.178.136.209/index.php?r=tbl-campus-building/modify-types",
                        {
                            'school_id':$state.params.campusid, 
                            'delete_types':JSON.stringify(delete_types),                   
                            'token':$localStorage.token                  

                        }
                    ).success(function (data) {
                      // console.log(data);
                        if(data.code==0&&data.msg=="修改成功"){
                            alert(data.msg);
                            $window.location.reload("app.sys.campusconfig")

                        }
                        if(data.code==1){
                            alert(data.msg);
                        }
                        if(data.code==20001&&data.msg=="请先登录"){
                            $location.path('access.signin');
                        }

                     }).error(function (data) {
                           alert(data);
                     })
                }
               
                $scope.ok = function () {
                    campus_name = $scope.oo.campus_name;
                    building_name = $scope.oo.building_name;
                    layer_amount = $scope.oo.layer_amount;
                    type =   $scope.oo.type;
                    layersJson =  $scope.oo.layersJson; 
                    responser =  $scope.oo.responser;
                    phone = $scope.oo.phone;
                    $modalInstance.close($scope.selected.item);
                };
                
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newcampusbuildingtypeModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-campus-building/create",
                        {
                            'school_id':$state.params.campusid,
                            'campus_name': campus_name,
                            'building_name': building_name,
                            'layer_amount': layer_amount,
                            'type':type,
                            'layersJson':layersJson,
                            'responser':responser,
                            'phone':phone,     
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=='楼创建成功'){
                        alert(data.msg);
                        $window.location.reload('app/sys/campusconfig');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])

app.controller('NewCampusbulidingLayerModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];     
            var  building_name, layer_amount,type,typeid;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.oo.type = $('#bulidingtype').val();
                $scope.oo.tyid = $('#typeid').val();
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };               
                $scope.oo.schoolname = $("#schoolname").text();
                $scope.ok = function () {
                    
                    building_name = $scope.oo.building_name;
                    layer_amount = $scope.oo.layer_amount;
                    typeid =   $scope.oo.tyid;
                    type = $scope.oo.type;

                   
                    $modalInstance.close($scope.selected.item);
                };
                
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newcampusbuildinglayerModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-campus-building/create",
                        {
                            'school_id':$state.params.campusid,                           
                            'building_name': building_name,
                            'layer_amount': layer_amount,
                            'type':typeid,                           
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=='楼创建成功'){
                        alert(data.msg);
                        $window.location.reload('app/sys/campusconfig'); 
                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])


app.controller('DelCampusbulidingModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$location','$localStorage',
        function ($scope, $modal, $log, $http, $window, $location,$localStorage) {

            $scope.items = ['item1', 'item2', 'item3'];
            var school_id,campus_building_id;            
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

                $scope.oo = {};

                $scope.items = items;               
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.del = function (hh) {
                //console.log(hh);
                school_id = parseInt(hh.school_id);  
                campus_building_id = parseInt(hh.campus_building_id);  
                $scope.open();
            }

            $scope.open = function (size) {

                var modalInstance = $modal.open({
                    templateUrl: 'delcampusbuildingModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;

                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {                   
                    $http.post(
                        $scope.app.backendurl+"tbl-campus-building/delete",
                        {
                           
                            'school_id': school_id,  
                            'campus_building_id':campus_building_id,                           
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="删除成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/campusconfig');

                      }
                      if(data.code==1){
                        alert(data.msg);
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })


                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            };
        }])